<!DOCTYPE html>
<html>
<head><meta name="generator" content="Hexo 3.9.0">
  <meta charset="utf-8">
  
  <title>Flink学习--如何计算实时热门商品 | 月迹的博客</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <meta name="description" content="首先我们把“实时热门商品”翻译成程序员易理解的语言：“每隔5分钟输出最近一小时内点击量最多的前N个商品”。将这个需求分解一下，将做如下几件事：  构建数据源 抽取业务数据时间戳，告诉Flink基于业务时间做窗口 过滤出点击行为数据 按1小时的窗口，每隔5分钟，做滑动窗口聚合 按每个窗口聚合，输出每个窗口中点击量前N的商品">
<meta name="keywords" content="flink,大数据,热门商品">
<meta property="og:type" content="article">
<meta property="og:title" content="Flink学习--如何计算实时热门商品">
<meta property="og:url" content="https://xyueji.github.io/Flink学习--如何计算实时热门商品.html">
<meta property="og:site_name" content="月迹的博客">
<meta property="og:description" content="首先我们把“实时热门商品”翻译成程序员易理解的语言：“每隔5分钟输出最近一小时内点击量最多的前N个商品”。将这个需求分解一下，将做如下几件事：  构建数据源 抽取业务数据时间戳，告诉Flink基于业务时间做窗口 过滤出点击行为数据 按1小时的窗口，每隔5分钟，做滑动窗口聚合 按每个窗口聚合，输出每个窗口中点击量前N的商品">
<meta property="og:locale" content="zh-CN">
<meta property="og:image" content="https://cdn.jsdelivr.net/gh/xyueji/img@master/web/D5DC7F51-58C5-45C7-9F10-15D513630F2B.png">
<meta property="og:updated_time" content="2020-07-04T18:19:19.360Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Flink学习--如何计算实时热门商品">
<meta name="twitter:description" content="首先我们把“实时热门商品”翻译成程序员易理解的语言：“每隔5分钟输出最近一小时内点击量最多的前N个商品”。将这个需求分解一下，将做如下几件事：  构建数据源 抽取业务数据时间戳，告诉Flink基于业务时间做窗口 过滤出点击行为数据 按1小时的窗口，每隔5分钟，做滑动窗口聚合 按每个窗口聚合，输出每个窗口中点击量前N的商品">
<meta name="twitter:image" content="https://cdn.jsdelivr.net/gh/xyueji/img@master/web/D5DC7F51-58C5-45C7-9F10-15D513630F2B.png">
  
    <link rel="alternative" href="/atom.xml" title="月迹的博客" type="application/atom+xml">
  
  
    <link rel="icon" href="/img/favicon.png">
  
  
      <link rel="stylesheet" href="//cdn.bootcss.com/animate.css/3.5.0/animate.min.css">
  
  <link rel="stylesheet" href="css/style.css">
  <link rel="stylesheet" href="/font-awesome/css/font-awesome.min.css">
  <link rel="apple-touch-icon" href="/apple-touch-icon.png">
  
  
      <link rel="stylesheet" href="/fancybox/jquery.fancybox.css">
  
    
  <!-- 加载特效 -->
    <script src="/js/pace.js"></script>
    <link href="/css/pace/pace-theme-flash.css" rel="stylesheet">
  <script>
      var yiliaConfig = {
          rootUrl: '/',
          fancybox: true,
          animate: true,
          isHome: false,
          isPost: true,
          isArchive: false,
          isTag: false,
          isCategory: false,
          open_in_new: false
      }
  </script>
    <script src="https://cdn.bootcdn.net/ajax/libs/gitalk/1.6.2/gitalk.min.js"></script>
    <script>
        (function(){
            var bp = document.createElement('script');
            var curProtocol = window.location.protocol.split(':')[0];
            if (curProtocol === 'https') {
                bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';
            }
            else {
                bp.src = 'http://push.zhanzhang.baidu.com/push.js';
            }
            var s = document.getElementsByTagName("script")[0];
            s.parentNode.insertBefore(bp, s);
        })();
    </script>
</head>
</html>
<body>
  <div id="container">
    <div class="left-col">
    <div class="overlay"></div>
<div class="intrude-less">
    <header id="header" class="inner">
        <a href="/" class="profilepic">
            
            <img lazy-src="/img/head.png" class="js-avatar">
            
        </a>

        <hgroup>
          <h1 class="header-author"><a href="/" title="Hi Mate">月 迹</a></h1>
        </hgroup>

        
        <p class="header-subtitle">用代码书写人生</p>
        
        
            <input type="text" class="search" id="search" style="text-align: center" placeholder="搜点什么...">
        
        
            <div id="switch-btn" class="switch-btn">
                <div class="icon">
                    <div class="icon-ctn">
                        <div class="icon-wrap icon-house" data-idx="0">
                            <div class="birdhouse"></div>
                            <div class="birdhouse_holes"></div>
                        </div>
                        <div class="icon-wrap icon-ribbon hide" data-idx="1">
                            <div class="ribbon"></div>
                        </div>
                        
                        <div class="icon-wrap icon-link hide" data-idx="2">
                            <div class="loopback_l"></div>
                            <div class="loopback_r"></div>
                        </div>
                        
                        
                        <div class="icon-wrap icon-me hide" data-idx="3">
                            <div class="user"></div>
                            <div class="shoulder"></div>
                        </div>
                        
                    </div>

                </div>
                <div class="tips-box hide">
                    <div class="tips-arrow"></div>
                    <ul class="tips-inner">
                        <li>菜单</li>
                        <li>标签</li>
                        
                        <li>友情链接</li>
                        
                        
                        <li>关于我</li>
                        
                    </ul>
                </div>
            </div>
        

        <div id="switch-area" class="switch-area">
            <div class="switch-wrap">
                <section class="switch-part switch-part1">
                    <nav class="header-menu">
                        <ul>
                        
                            <li><a href="index.html">博客首页</a></li>
                        
                            <li><a href="/archives">文章归档</a></li>
                        
                            <li><a href="/tags">文章统计</a></li>
                        
                            <li><a href="/message">交友留言</a></li>
                        
                            <li><a href="/navigation">资源导航</a></li>
                        
                            <li><a href="/about">关于作者</a></li>
                        
                        </ul>
                    </nav>
                    <nav class="header-nav">
                        <ul class="social">
                            
                                <a class="fl mail" target="_blank" href="http://mail.qq.com/cgi-bin/qm_share?t=qm_mailme&email=U2FhYmdrZGVjamITIiJ9MDw_" title="mail">mail</a>
                            
                                <a class="fl github" target="_blank" href="https://github.com/xyueji" title="github">github</a>
                            
                                <a class="fl rss" target="_blank" href="/atom.xml" title="rss">rss</a>
                            
                        </ul>
                    </nav>
                </section>

                
                <section class="switch-part switch-part2">
                    <div class="widget tagcloud" id="js-tagcloud">
                        <a href="tags/AppendTrie树/" style="font-size: 10px;">AppendTrie树</a> <a href="tags/HDFS/" style="font-size: 10px;">HDFS</a> <a href="tags/JanusGraph/" style="font-size: 10px;">JanusGraph</a> <a href="tags/SQL解析/" style="font-size: 10px;">SQL解析</a> <a href="tags/SQL采集/" style="font-size: 10px;">SQL采集</a> <a href="tags/alluxio/" style="font-size: 10px;">alluxio</a> <a href="tags/bitmap/" style="font-size: 10px;">bitmap</a> <a href="tags/container/" style="font-size: 10px;">container</a> <a href="tags/datasource/" style="font-size: 10px;">datasource</a> <a href="tags/docker/" style="font-size: 15px;">docker</a> <a href="tags/docker-compose/" style="font-size: 15px;">docker-compose</a> <a href="tags/flink/" style="font-size: 15px;">flink</a> <a href="tags/free/" style="font-size: 10px;">free</a> <a href="tags/git/" style="font-size: 10px;">git</a> <a href="tags/grafana/" style="font-size: 10px;">grafana</a> <a href="tags/hadoop/" style="font-size: 10px;">hadoop</a> <a href="tags/hbase/" style="font-size: 15px;">hbase</a> <a href="tags/hbck/" style="font-size: 10px;">hbck</a> <a href="tags/hive/" style="font-size: 15px;">hive</a> <a href="tags/idea/" style="font-size: 15px;">idea</a> <a href="tags/impala/" style="font-size: 10px;">impala</a> <a href="tags/iptables/" style="font-size: 10px;">iptables</a> <a href="tags/mac/" style="font-size: 10px;">mac</a> <a href="tags/mapreduce/" style="font-size: 10px;">mapreduce</a> <a href="tags/metrics2/" style="font-size: 10px;">metrics2</a> <a href="tags/opentsdb/" style="font-size: 10px;">opentsdb</a> <a href="tags/phoenix/" style="font-size: 15px;">phoenix</a> <a href="tags/presto/" style="font-size: 10px;">presto</a> <a href="tags/prometheus/" style="font-size: 10px;">prometheus</a> <a href="tags/pycharm/" style="font-size: 10px;">pycharm</a> <a href="tags/salt-api/" style="font-size: 10px;">salt-api</a> <a href="tags/shuffle/" style="font-size: 15px;">shuffle</a> <a href="tags/spark/" style="font-size: 15px;">spark</a> <a href="tags/springboot/" style="font-size: 10px;">springboot</a> <a href="tags/top/" style="font-size: 10px;">top</a> <a href="tags/vue/" style="font-size: 10px;">vue</a> <a href="tags/wordcount/" style="font-size: 10px;">wordcount</a> <a href="tags/修复工具/" style="font-size: 10px;">修复工具</a> <a href="tags/元数据/" style="font-size: 10px;">元数据</a> <a href="tags/分片/" style="font-size: 10px;">分片</a> <a href="tags/基础知识/" style="font-size: 10px;">基础知识</a> <a href="tags/大数据/" style="font-size: 20px;">大数据</a> <a href="tags/大文件/" style="font-size: 10px;">大文件</a> <a href="tags/大查询/" style="font-size: 10px;">大查询</a> <a href="tags/安装/" style="font-size: 10px;">安装</a> <a href="tags/小文件/" style="font-size: 10px;">小文件</a> <a href="tags/开发规范/" style="font-size: 10px;">开发规范</a> <a href="tags/数据倾斜/" style="font-size: 10px;">数据倾斜</a> <a href="tags/数据血缘/" style="font-size: 10px;">数据血缘</a> <a href="tags/测试/" style="font-size: 10px;">测试</a> <a href="tags/热门商品/" style="font-size: 10px;">热门商品</a> <a href="tags/端口/" style="font-size: 10px;">端口</a> <a href="tags/端口映射/" style="font-size: 10px;">端口映射</a> <a href="tags/精准去重/" style="font-size: 10px;">精准去重</a> <a href="tags/系统表/" style="font-size: 10px;">系统表</a> <a href="tags/表重建/" style="font-size: 10px;">表重建</a>
                    </div>
                </section>
                

                
                <section class="switch-part switch-part3">
                    <div id="js-friends">
                    
                      <a target="_blank" class="main-nav-link switch-friends-link" href="https://dayesahh.github.io/">DaYesahh</a>
                    
                      <a target="_blank" class="main-nav-link switch-friends-link" href="http://www.54tianzhisheng.cn/">zhisjeng</a>
                    
                    </div>
                </section>
                

                
                
                <section class="switch-part switch-part4">
                
                    <div id="js-aboutme">爱动漫、追小说、喜影视、迷于严谨和逻辑的编码之中！！！</div>
                </section>
                
            </div>
        </div>
    </header>
</div>

    </div>
    <div class="mid-col">
      <nav id="mobile-nav">
      <div class="overlay">
          <div class="slider-trigger"></div>
          <h1 class="header-author js-mobile-header hide"><a href="/" title="Me">月 迹</a></h1>
      </div>
    <div class="intrude-less">
        <header id="header" class="inner">
            <a href="/" class="profilepic">
                
                    <img lazy-src="/img/head.png" class="js-avatar">
                
            </a>
            <hgroup>
              <h1 class="header-author"><a href="/" title="Me">月 迹</a></h1>
            </hgroup>
            
            <p class="header-subtitle">用代码书写人生</p>
            
            <nav class="header-menu">
                <ul>
                
                    <li><a href="index.html">博客首页</a></li>
                
                    <li><a href="/archives">文章归档</a></li>
                
                    <li><a href="/tags">文章统计</a></li>
                
                    <li><a href="/message">交友留言</a></li>
                
                    <li><a href="/navigation">资源导航</a></li>
                
                    <li><a href="/about">关于作者</a></li>
                
                <div class="clearfix"></div>
                </ul>
            </nav>
            <nav class="header-nav">
                <div class="social">
                    
                        <a class="mail" target="_blank" href="http://mail.qq.com/cgi-bin/qm_share?t=qm_mailme&email=U2FhYmdrZGVjamITIiJ9MDw_" title="mail">mail</a>
                    
                        <a class="github" target="_blank" href="https://github.com/xyueji" title="github">github</a>
                    
                        <a class="rss" target="_blank" href="/atom.xml" title="rss">rss</a>
                    
                </div>
            </nav>
        </header>                
    </div>
</nav>
      <div class="body-wrap"><article id="post-Flink学习--如何计算实时热门商品" class="article article-type-post" itemscope itemprop="blogPost">
    
    <div class="article-meta">
      <a href="" class="article-date">
      <time datetime="2020-06-16T09:30:44.000Z" itemprop="datePublished">2020-06-16</time>
</a>
    </div>
  
  <div class="article-inner">
    
      <input type="hidden" class="isFancy" />
    
    
      <header class="article-header">
        
  
    <h1 class="article-title" itemprop="name">
      Flink学习--如何计算实时热门商品
    </h1>
  

      </header>
      
      <div class="article-info article-info-post">
        
    <div class="article-category tagcloud">
    <a class="article-category-link" href="categories/flink/">flink</a>
    </div>


        
    <div class="article-tag tagcloud">
        <ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="tags/flink/">flink</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="tags/大数据/">大数据</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="tags/热门商品/">热门商品</a></li></ul>
    </div>

        <div class="clearfix"></div>
      </div>
      
    
    <div class="article-entry" itemprop="articleBody">
      
          
        <p>首先我们把“实时热门商品”翻译成程序员易理解的语言：“每隔5分钟输出最近一小时内点击量最多的前N个商品”。将这个需求分解一下，将做如下几件事：</p>
<ul>
<li>构建数据源</li>
<li>抽取业务数据时间戳，告诉Flink基于业务时间做窗口</li>
<li>过滤出点击行为数据</li>
<li>按1小时的窗口，每隔5分钟，做滑动窗口聚合</li>
<li>按每个窗口聚合，输出每个窗口中点击量前N的商品</li>
</ul>
<a id="more"></a>
<h2 id="构建数据源"><a href="#构建数据源" class="headerlink" title="构建数据源"></a>构建数据源</h2><p>本文数据为淘宝用户行为数据集（来自<a href="https://link.juejin.im/?target=https%3A%2F%2Ftianchi.aliyun.com%2Fdatalab%2Findex.htm" target="_blank" rel="noopener">阿里云天池公开数据集</a>，特别感谢）。本数据集包含了淘宝上某一天随机一百万用户的所有行为（包括点击、购买、加购、收藏）。数据集的组织形式和MovieLens-20M类似，即数据集的每一行表示一条用户行为，由用户ID、商品ID、商品类目ID、行为类型和时间戳组成，并以逗号分隔。关于数据集中每一列的详细描述如下：</p>
<table>
<thead>
<tr>
<th>列名称</th>
<th>说明</th>
</tr>
</thead>
<tbody>
<tr>
<td>用户ID</td>
<td>整数类型，加密后的用户ID</td>
</tr>
<tr>
<td>商品ID</td>
<td>整数类型，加密后的商品ID</td>
</tr>
<tr>
<td>商品类目ID</td>
<td>整数类型，加密后的商品所属类目ID</td>
</tr>
<tr>
<td>行为类型</td>
<td>字符串，枚举类型，包括(‘pv’, ‘buy’, ‘cart’, ‘fav’)</td>
</tr>
<tr>
<td>时间戳</td>
<td>行为发生的时间戳，单位秒</td>
</tr>
</tbody>
</table>
<p>下载数据集，放入项目resource目录<br><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">cd flink-learning-project/src/main/resources</span><br><span class="line">curl https://raw.githubusercontent.com/wuchong/my-flink-project/master/src/main/resources/UserBehavior.csv &gt; UserBehavior.csv</span><br></pre></td></tr></table></figure></p>
<p>创建数据集实体类，UserBehavior.class<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.xyueji.flink.core.model;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span> xiongzhigang</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@date</span> 2020-06-05 18:19</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@description</span></span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">UserBehavior</span> </span>&#123;</span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">long</span> userId;</span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">long</span> itemId;</span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">int</span> categoryId;</span><br><span class="line">    <span class="keyword">private</span> String behavior;</span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">long</span> timestamp;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> String <span class="title">toString</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="string">"UserBehavior&#123;"</span> +</span><br><span class="line">                <span class="string">"userId="</span> + userId +</span><br><span class="line">                <span class="string">", itemId="</span> + itemId +</span><br><span class="line">                <span class="string">", categoryId="</span> + categoryId +</span><br><span class="line">                <span class="string">", behavior='"</span> + behavior + <span class="string">'\''</span> +</span><br><span class="line">                <span class="string">", timestamp="</span> + timestamp +</span><br><span class="line">                <span class="string">'&#125;'</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">long</span> <span class="title">getUserId</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> userId;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setUserId</span><span class="params">(<span class="keyword">long</span> userId)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">this</span>.userId = userId;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">long</span> <span class="title">getItemId</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> itemId;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setItemId</span><span class="params">(<span class="keyword">long</span> itemId)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">this</span>.itemId = itemId;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">getCategoryId</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> categoryId;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setCategoryId</span><span class="params">(<span class="keyword">int</span> categoryId)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">this</span>.categoryId = categoryId;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> String <span class="title">getBehavior</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> behavior;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setBehavior</span><span class="params">(String behavior)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">this</span>.behavior = behavior;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">long</span> <span class="title">getTimestamp</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> timestamp;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setTimestamp</span><span class="params">(<span class="keyword">long</span> timestamp)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">this</span>.timestamp = timestamp;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>编写Flink主程序，com.xyueji.flink.project.topnhot.HotItems<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">HotItems</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">    </span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>读取数据集，构建Flink source：<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// UserBehavior.csv本地路径</span></span><br><span class="line">URL url = HotItems.class.getClassLoader().getResource(<span class="string">"UserBehavior.csv"</span>);</span><br><span class="line">Path localFile = Path.fromLocalFile(<span class="keyword">new</span> File(url.toURI()));</span><br><span class="line"></span><br><span class="line">PojoTypeInfo pojoTypeInfo = (PojoTypeInfo) TypeExtractor.createTypeInfo(UserBehavior.class);</span><br><span class="line"><span class="comment">// 字段顺序</span></span><br><span class="line">String[] fieldNames = <span class="keyword">new</span> String[]&#123;<span class="string">"userId"</span>, <span class="string">"itemId"</span>, <span class="string">"categoryId"</span>, <span class="string">"behavior"</span>, <span class="string">"timestamp"</span>&#125;;</span><br><span class="line"><span class="comment">// csv InputFormat</span></span><br><span class="line">PojoCsvInputFormat csvInputFormat = <span class="keyword">new</span> PojoCsvInputFormat&lt;&gt;(localFile, pojoTypeInfo, fieldNames);</span><br><span class="line"><span class="comment">// 创建Flink执行环境</span></span><br><span class="line">StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();</span><br><span class="line"><span class="comment">// 模拟真实数据流，这里并行度设为1</span></span><br><span class="line">env.setParallelism(<span class="number">1</span>);</span><br><span class="line"><span class="comment">// 添加source</span></span><br><span class="line">DataStreamSource dataStreamSource = env.createInput(csvInputFormat, pojoTypeInfo);</span><br></pre></td></tr></table></figure></p>
<h2 id="EventTime-与-Watermark"><a href="#EventTime-与-Watermark" class="headerlink" title="EventTime 与 Watermark"></a>EventTime 与 Watermark</h2><p>抽取业务数据时间戳，设置EventTime，以业务时间做窗口。这里说明一下：</p>
<ul>
<li>ProcessingTime：事件被处理的时间。也就是由机器的系统时间来决定。</li>
<li>EventTime：事件发生的时间。一般就是数据本身携带的时间。<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 这里要设置EventTime, 默认ProcessingTime</span></span><br><span class="line">env.setStreamTimeCharacteristic(TimeCharacteristic.EventTime);</span><br><span class="line"></span><br><span class="line">DataStream timeData = dataStreamSource.assignTimestampsAndWatermarks(<span class="keyword">new</span> AscendingTimestampExtractor&lt;UserBehavior&gt;() &#123;</span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">long</span> <span class="title">extractAscendingTimestamp</span><span class="params">(UserBehavior userBehavior)</span> </span>&#123;</span><br><span class="line">        <span class="comment">// 数据集时间单位为秒，转换为毫秒</span></span><br><span class="line">        <span class="keyword">return</span> userBehavior.getTimestamp() * <span class="number">1000</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>
</li>
</ul>
<h2 id="过滤"><a href="#过滤" class="headerlink" title="过滤"></a>过滤</h2><p>再来回顾一下，”每隔5分钟输出最近1小时点击量前N的商品“，我们先把点击这个行为类型的数据过滤出来。<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">DataStream pvData = timeData.filter(<span class="keyword">new</span> FilterFunction&lt;UserBehavior&gt;() &#123;</span><br><span class="line">            <span class="meta">@Override</span></span><br><span class="line">            <span class="function"><span class="keyword">public</span> <span class="keyword">boolean</span> <span class="title">filter</span><span class="params">(UserBehavior userBehavior)</span> <span class="keyword">throws</span> Exception </span>&#123;</span><br><span class="line">                <span class="keyword">return</span> userBehavior.getBehavior().equals(<span class="string">"pv"</span>);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;);</span><br></pre></td></tr></table></figure></p>
<h2 id="滑动窗口"><a href="#滑动窗口" class="headerlink" title="滑动窗口"></a>滑动窗口</h2><p>按1小时做窗口，每隔5分钟，做滑动窗口聚合。<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">DataStream windowDate = pvData</span><br><span class="line">                .keyBy(<span class="string">"itemId"</span>) <span class="comment">//以itemId分组</span></span><br><span class="line">                .timeWindow(Time.minutes(<span class="number">60</span>), Time.minutes(<span class="number">5</span>))</span><br><span class="line">                .aggregate(<span class="keyword">new</span> CountAgg(), <span class="keyword">new</span> WindowResultFunction());</span><br></pre></td></tr></table></figure></p>
<p>这里的CountAgg实现了AggregateFunction接口，功能是统计窗口中的条数，即遇到一条数据就加一。<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">static</span> <span class="class"><span class="keyword">class</span> <span class="title">CountAgg</span> <span class="keyword">implements</span> <span class="title">AggregateFunction</span>&lt;<span class="title">UserBehavior</span>, <span class="title">Long</span>, <span class="title">Long</span>&gt; </span>&#123;</span><br><span class="line"></span><br><span class="line">        <span class="meta">@Override</span></span><br><span class="line">        <span class="function"><span class="keyword">public</span> Long <span class="title">createAccumulator</span><span class="params">()</span> </span>&#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="number">0L</span>;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="meta">@Override</span></span><br><span class="line">        <span class="function"><span class="keyword">public</span> Long <span class="title">add</span><span class="params">(UserBehavior userBehavior, Long acc)</span> </span>&#123;</span><br><span class="line">            <span class="keyword">return</span> acc + <span class="number">1</span>;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="meta">@Override</span></span><br><span class="line">        <span class="function"><span class="keyword">public</span> Long <span class="title">getResult</span><span class="params">(Long acc)</span> </span>&#123;</span><br><span class="line">            <span class="keyword">return</span> acc;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="meta">@Override</span></span><br><span class="line">        <span class="function"><span class="keyword">public</span> Long <span class="title">merge</span><span class="params">(Long acc1, Long acc2)</span> </span>&#123;</span><br><span class="line">            <span class="keyword">return</span> acc1 + acc2;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure></p>
<p>WindowResultFunction实现了WindowFunction接口，将每个key每个窗口聚合后的结果带上其他信息进行输出。我们这里实现的WindowResultFunction将主键商品ID，窗口，点击量封装成了ItemViewCount进行输出。<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">static</span> <span class="class"><span class="keyword">class</span> <span class="title">WindowResultFunction</span> <span class="keyword">implements</span> <span class="title">WindowFunction</span>&lt;<span class="title">Long</span>, <span class="title">ItemViewCount</span>, <span class="title">Tuple</span>, <span class="title">TimeWindow</span>&gt; </span>&#123;</span><br><span class="line"></span><br><span class="line">        <span class="meta">@Override</span></span><br><span class="line">        <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">apply</span><span class="params">(</span></span></span><br><span class="line"><span class="function"><span class="params">                Tuple key,  // 窗口的主键，即 itemId</span></span></span><br><span class="line"><span class="function"><span class="params">                TimeWindow window,  // 窗口</span></span></span><br><span class="line"><span class="function"><span class="params">                Iterable&lt;Long&gt; aggregateResult, // 聚合函数的结果，即 count 值</span></span></span><br><span class="line"><span class="function"><span class="params">                Collector&lt;ItemViewCount&gt; collector  // 输出类型为 ItemViewCount</span></span></span><br><span class="line"><span class="function"><span class="params">        )</span> <span class="keyword">throws</span> Exception </span>&#123;</span><br><span class="line">            Long itemId = (Long) ((Tuple1) key).f0;</span><br><span class="line">            Long count = aggregateResult.iterator().next();</span><br><span class="line">            collector.collect(ItemViewCount.of(itemId, window.getEnd(), count));</span><br><span class="line">        &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 商品点击量(窗口操作的输出类型)</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">static</span> <span class="class"><span class="keyword">class</span> <span class="title">ItemViewCount</span> </span>&#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">long</span> itemId;     <span class="comment">// 商品ID</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">long</span> windowEnd;  <span class="comment">// 窗口结束时间戳</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">long</span> viewCount;  <span class="comment">// 商品的点击量</span></span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> ItemViewCount <span class="title">of</span><span class="params">(<span class="keyword">long</span> itemId, <span class="keyword">long</span> windowEnd, <span class="keyword">long</span> viewCount)</span> </span>&#123;</span><br><span class="line">        ItemViewCount result = <span class="keyword">new</span> ItemViewCount();</span><br><span class="line">        result.itemId = itemId;</span><br><span class="line">        result.windowEnd = windowEnd;</span><br><span class="line">        result.viewCount = viewCount;</span><br><span class="line">        <span class="keyword">return</span> result;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<h2 id="TopN-计算最热门商品"><a href="#TopN-计算最热门商品" class="headerlink" title="TopN 计算最热门商品"></a>TopN 计算最热门商品</h2><p>进行窗口聚合后，需要将窗口进行分组，这里根据ItemViewCount中的windowEnd进行keyBy分组，然后使用ProcessFunction计算每个窗口的topN的商品。<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">DataStream topItems = windowDate.keyBy(<span class="string">"windowEnd"</span>).process(<span class="keyword">new</span> TopNHotItems(<span class="number">5</span>));</span><br></pre></td></tr></table></figure></p>
<p>ProcessFunction 是 Flink 提供的一个 low-level API，用于实现更高级的功能。它主要提供了定时器 timer 的功能（支持EventTime或ProcessingTime）。本案例中我们将利用 timer 来判断何时收齐了某个 window 下所有商品的点击量数据。由于 Watermark 的进度是全局的，</p>
<p>在 processElement 方法中，每当收到一条数据（ItemViewCount），我们就注册一个 windowEnd+1 的定时器（Flink 框架会自动忽略同一时间的重复注册）。windowEnd+1 的定时器被触发时，意味着收到了windowEnd+1的 Watermark，即收齐了该windowEnd下的所有商品窗口统计值。我们在 onTimer() 中处理将收集的所有商品及点击量进行排序，选出 TopN，并将排名信息格式化成字符串后进行输出。</p>
<p>这里我们还使用了 ListState<itemviewcount> 来存储收到的每条 ItemViewCount 消息，保证在发生故障时，状态数据的不丢失和一致性。ListState 是 Flink 提供的类似 Java List 接口的 State API，它集成了框架的 checkpoint 机制，自动做到了 exactly-once 的语义保证。<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 求某个窗口中前 N 名的热门点击商品，key 为窗口时间戳，输出为 TopN 的结果字符串</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">static</span> <span class="class"><span class="keyword">class</span> <span class="title">TopNHotItems</span> <span class="keyword">extends</span> <span class="title">KeyedProcessFunction</span>&lt;<span class="title">Tuple</span>, <span class="title">ItemViewCount</span>, <span class="title">String</span>&gt; </span>&#123;</span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">final</span> <span class="keyword">int</span> topSize;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="title">TopNHotItems</span><span class="params">(<span class="keyword">int</span> topSize)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">this</span>.topSize = topSize;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// 用于存储商品与点击数的状态，待收齐同一个窗口的数据后，再触发 TopN 计算</span></span><br><span class="line">    <span class="keyword">private</span> ListState&lt;ItemViewCount&gt; itemState;</span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">open</span><span class="params">(Configuration parameters)</span> <span class="keyword">throws</span> Exception </span>&#123;</span><br><span class="line">        <span class="keyword">super</span>.open(parameters);</span><br><span class="line">        <span class="comment">// 状态的注册</span></span><br><span class="line">        ListStateDescriptor&lt;ItemViewCount&gt; itemsStateDesc = <span class="keyword">new</span> ListStateDescriptor&lt;&gt;(</span><br><span class="line">                <span class="string">"itemState-state"</span>,</span><br><span class="line">                ItemViewCount.class);</span><br><span class="line">        itemState = getRuntimeContext().getListState(itemsStateDesc);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">processElement</span><span class="params">(</span></span></span><br><span class="line"><span class="function"><span class="params">            ItemViewCount input,</span></span></span><br><span class="line"><span class="function"><span class="params">            Context context,</span></span></span><br><span class="line"><span class="function"><span class="params">            Collector&lt;String&gt; collector)</span> <span class="keyword">throws</span> Exception </span>&#123;</span><br><span class="line">        <span class="comment">// 每条数据都保存到状态中</span></span><br><span class="line">        itemState.add(input);</span><br><span class="line">        <span class="comment">// 注册 windowEnd+1 的 EventTime Timer, 当触发时，说明收齐了属于windowEnd窗口的所有商品数据</span></span><br><span class="line">        context.timerService().registerEventTimeTimer(input.windowEnd + <span class="number">1</span>);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">onTimer</span><span class="params">(</span></span></span><br><span class="line"><span class="function"><span class="params">            <span class="keyword">long</span> timestamp, OnTimerContext ctx, Collector&lt;String&gt; out)</span> <span class="keyword">throws</span> Exception </span>&#123;</span><br><span class="line">        <span class="comment">// 获取收到的所有商品点击量</span></span><br><span class="line">        List&lt;ItemViewCount&gt; allItems = <span class="keyword">new</span> ArrayList&lt;&gt;();</span><br><span class="line">        <span class="keyword">for</span> (ItemViewCount item : itemState.get()) &#123;</span><br><span class="line">            allItems.add(item);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">// 提前清除状态中的数据，释放空间</span></span><br><span class="line">        itemState.clear();</span><br><span class="line">        <span class="comment">// 按照点击量从大到小排序</span></span><br><span class="line">        allItems.sort(<span class="keyword">new</span> Comparator&lt;ItemViewCount&gt;() &#123;</span><br><span class="line">            <span class="meta">@Override</span></span><br><span class="line">            <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">compare</span><span class="params">(ItemViewCount o1, ItemViewCount o2)</span> </span>&#123;</span><br><span class="line">                <span class="keyword">return</span> (<span class="keyword">int</span>) (o2.viewCount - o1.viewCount);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;);</span><br><span class="line">        <span class="comment">// 将排名信息格式化成 String, 便于打印</span></span><br><span class="line">        StringBuilder result = <span class="keyword">new</span> StringBuilder();</span><br><span class="line">        result.append(<span class="string">"====================================\n"</span>);</span><br><span class="line">        result.append(<span class="string">"时间: "</span>).append(<span class="keyword">new</span> Timestamp(timestamp - <span class="number">1</span>)).append(<span class="string">"\n"</span>);</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; topSize; i++) &#123;</span><br><span class="line">            ItemViewCount currentItem = allItems.get(i);</span><br><span class="line">            <span class="comment">// No1:  商品ID=12224  浏览量=2413</span></span><br><span class="line">            result.append(<span class="string">"No"</span>).append(i).append(<span class="string">":"</span>)</span><br><span class="line">                    .append(<span class="string">"  商品ID="</span>).append(currentItem.itemId)</span><br><span class="line">                    .append(<span class="string">"  浏览量="</span>).append(currentItem.viewCount)</span><br><span class="line">                    .append(<span class="string">"\n"</span>);</span><br><span class="line">        &#125;</span><br><span class="line">        result.append(<span class="string">"====================================\n\n"</span>);</span><br><span class="line">        <span class="comment">// 控制输出频率，模拟实时滚动结果</span></span><br><span class="line">        Thread.sleep(<span class="number">1000</span>);</span><br><span class="line">        out.collect(result.toString());</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></itemviewcount></p>
<h2 id="打印"><a href="#打印" class="headerlink" title="打印"></a>打印</h2><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">topItems.print();</span><br></pre></td></tr></table></figure>
<h2 id="执行"><a href="#执行" class="headerlink" title="执行"></a>执行</h2><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">env.execute("Hot Items Job");</span><br></pre></td></tr></table></figure>
<h2 id="执行结果"><a href="#执行结果" class="headerlink" title="执行结果"></a>执行结果</h2><p><img src="https://cdn.jsdelivr.net/gh/xyueji/img@master/web/D5DC7F51-58C5-45C7-9F10-15D513630F2B.png" alt="616ed78c549665c8dfabe75ef53884b7"></p>
<p>具体代码见：<a href="https://github.com/xyueji/flink-learning" target="_blank" rel="noopener">https://github.com/xyueji/flink-learning</a></p>

      
      
        <div class="page-reward">
          <p><a href="javascript:void(0)" onclick="dashangToggle()" class="dashang">赏</a></p>
          <div class="hide_box"></div>
          <div class="shang_box">
            <a class="shang_close" href="javascript:void(0)" onclick="dashangToggle()">×</a>
            <div class="shang_tit">
              <p>纯属好玩</p>
            </div>
            <div class="shang_payimg">
              <img src="/img/alipayimg.jpg" alt="扫码支持" title="扫一扫" />
            </div>
              <div class="pay_explain">扫码打赏，你说多少就多少</div>
            <div class="shang_payselect">
              
                <div class="pay_item checked" data-id="alipay">
                  <span class="radiobox"></span>
                  <span class="pay_logo"><img src="/img/alipay.png" alt="支付宝" /></span>
                </div>
              
              
                <div class="pay_item" data-id="wechat">
                  <span class="radiobox"></span>
                  <span class="pay_logo"><img src="/img/weixin.png" alt="微信" /></span>
                </div>
              
            </div>
            <div class="shang_info">
              <p>打开<span id="shang_pay_txt">支付宝</span>扫一扫，即可进行扫码打赏哦</p>
            </div>
          </div>
        </div>
        <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/zepto/1.2.0/zepto.min.js"></script>
        <script type="text/javascript">
          $(".pay_item").click(function(){
            $(this).addClass('checked').siblings('.pay_item').removeClass('checked');
            var dataid=$(this).attr('data-id');
            $(".shang_payimg img").attr("src","/img/"+dataid+"img.jpg");
            $("#shang_pay_txt").text(dataid=="alipay"?"支付宝":"微信");
          });
          function dashangToggle(){

            $(".hide_box").fadeToggle();
            $(".shang_box").fadeToggle();
          }
        </script>
      
    </div>
    
  </div>
  
    
    <div class="copyright">
        <p><span>本文标题:</span><a href="">Flink学习--如何计算实时热门商品</a></p>
        <p><span>文章作者:</span><a href="/" title="访问 月 迹 的个人博客">月 迹</a></p>
        <p><span>发布时间:</span>2020年06月16日 - 17时30分</p>
        <p><span>最后更新:</span>2020年07月05日 - 02时19分</p>
        <p>
            <span>原始链接:</span><a class="post-url" href="" title="Flink学习--如何计算实时热门商品">https://xyueji.github.io/Flink学习--如何计算实时热门商品.html</a>
            <span class="copy-path" data-clipboard-text="原文: https://xyueji.github.io/Flink学习--如何计算实时热门商品.html　　作者: 月 迹" title="点击复制文章链接"><i class="fa fa-clipboard"></i></span>
            <script src="/js/clipboard.min.js"></script>
            <script> var clipboard = new Clipboard('.copy-path'); </script>
        </p>
        <p>
            <span>许可协议:</span><i class="fa fa-creative-commons"></i> <a rel="license" href="http://creativecommons.org/licenses/by-nc-sa/3.0/cn/" title="中国大陆 (CC BY-NC-SA 3.0 CN)" target = "_blank">"署名-非商用-相同方式共享 3.0"</a> 转载请保留原文链接及作者。
        </p>
    </div>



<nav id="article-nav">
  
    <a href="Git开发规范.html" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption"><</strong>
      <div class="article-nav-title">
        
          Git开发规范
        
      </div>
    </a>
  
  
    <a href="Flink学习--基本概念与环境搭建.html" id="article-nav-older" class="article-nav-link-wrap">
      <div class="article-nav-title">Flink学习--基本概念与环境搭建</div>
      <strong class="article-nav-caption">></strong>
    </a>
  
</nav>


  
</article>

    <div id="toc" class="toc-article">
    <strong class="toc-title">文章目录</strong>
    <ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#构建数据源"><span class="toc-number">1.</span> <span class="toc-text">构建数据源</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#EventTime-与-Watermark"><span class="toc-number">2.</span> <span class="toc-text">EventTime 与 Watermark</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#过滤"><span class="toc-number">3.</span> <span class="toc-text">过滤</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#滑动窗口"><span class="toc-number">4.</span> <span class="toc-text">滑动窗口</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#TopN-计算最热门商品"><span class="toc-number">5.</span> <span class="toc-text">TopN 计算最热门商品</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#打印"><span class="toc-number">6.</span> <span class="toc-text">打印</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#执行"><span class="toc-number">7.</span> <span class="toc-text">执行</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#执行结果"><span class="toc-number">8.</span> <span class="toc-text">执行结果</span></a></li></ol>
</div>
<input type="button" id="tocButton" value="隐藏目录"  title="点击按钮隐藏或者显示文章目录">

<script src="https://cdnjs.cloudflare.com/ajax/libs/require.js/2.1.6/require.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script>
    var valueHide = "隐藏目录";
    var valueShow = "显示目录";

    if ($(".left-col").is(":hidden")) {
        $("#tocButton").attr("value", valueShow);
    }
    $("#tocButton").click(function() {
        if ($("#toc").is(":hidden")) {
            $("#tocButton").attr("value", valueHide);
            $("#toc").slideDown(320);
        }
        else {
            $("#tocButton").attr("value", valueShow);
            $("#toc").slideUp(350);
        }
    })
    if ($(".toc").length < 1) {
        $("#toc, #tocButton").hide();
    }
</script>






<div class="bdsharebuttonbox">
	<a href="#" class="fx fa-weibo bds_tsina" data-cmd="tsina" title="分享到新浪微博"></a>
	<a href="#" class="fx fa-weixin bds_weixin" data-cmd="weixin" title="分享到微信"></a>
	<a href="#" class="fx fa-qq bds_sqq" data-cmd="sqq" title="分享到QQ好友"></a>
	<a href="#" class="fx fa-facebook-official bds_fbook" data-cmd="fbook" title="分享到Facebook"></a>
	<a href="#" class="fx fa-twitter bds_twi" data-cmd="twi" title="分享到Twitter"></a>
	<a href="#" class="fx fa-linkedin bds_linkedin" data-cmd="linkedin" title="分享到linkedin"></a>
	<a href="#" class="fx fa-files-o bds_copy" data-cmd="copy" title="分享到复制网址"></a>
</div>
<script>window._bd_share_config={"common":{"bdSnsKey":{},"bdText":"","bdMini":"2","bdMiniList":false,"bdPic":"","bdStyle":"2","bdSize":"24"},"share":{}};with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='/static/api/js/share.js?v=89860593.js?cdnversion='+~(-new Date()/36e5)];</script>




    
        <link rel="stylesheet" href="https://cdn.bootcdn.net/ajax/libs/gitalk/1.6.2/gitalk.min.css">
<div id="gitalk-container" class="gitment-container"></div>
<script type="text/javascript">
    var gitalk = new Gitalk({
        clientID: '8864436c2b03c3753a80',
        clientSecret: '252abc963e694743b034294b00a36ec6c7ee2152',
        id: 'Flink学习--如何计算实时热门商品.html',
        owner: 'xyueji',
        repo: 'xyueji.github.io',
        admin: 'xyueji',
        proxy: 'https://still-shadow-c49f.xyueji.workers.dev/?https://github.com/login/oauth/access_token'
    })
    gitalk.render('gitalk-container')
</script>

    



    <div class="scroll" id="post-nav-button">
        
            <a href="Git开发规范.html" title="上一篇: Git开发规范">
                <i class="fa fa-angle-left"></i>
            </a>
        
        <a title="文章列表"><i class="fa fa-bars"></i><i class="fa fa-times"></i></a>
        
            <a href="Flink学习--基本概念与环境搭建.html" title="下一篇: Flink学习--基本概念与环境搭建">
                <i class="fa fa-angle-right"></i>
            </a>
        
    </div>
    <ul class="post-list"><li class="post-list-item"><a class="post-list-link" href="统计HDFS元数据.html">统计HDFS元数据</a></li><li class="post-list-item"><a class="post-list-link" href="元数据平台-数据血缘.html">元数据平台-数据血缘</a></li><li class="post-list-item"><a class="post-list-link" href="精准去重-字典编码+Bitmap.html">精准去重-字典编码+Bitmap</a></li><li class="post-list-item"><a class="post-list-link" href="大数据监控-Metrics2录入指标到OpenTSDB.html">大数据监控-Metrics2录入指标到OpenTSDB</a></li><li class="post-list-item"><a class="post-list-link" href="Spark解决数据倾斜问题.html">Spark解决数据倾斜问题</a></li><li class="post-list-item"><a class="post-list-link" href="Spark-Shuffle原理.html">Spark-Shuffle原理</a></li><li class="post-list-item"><a class="post-list-link" href="MapReduce-Shuffle过程.html">MapReduce-Shuffle过程</a></li><li class="post-list-item"><a class="post-list-link" href="Git开发规范.html">Git开发规范</a></li><li class="post-list-item"><a class="post-list-link" href="">Flink学习--如何计算实时热门商品</a></li><li class="post-list-item"><a class="post-list-link" href="Flink学习--基本概念与环境搭建.html">Flink学习--基本概念与环境搭建</a></li><li class="post-list-item"><a class="post-list-link" href="docker-compose搭建大数据环境.html">docker-compose搭建大数据环境</a></li><li class="post-list-item"><a class="post-list-link" href="Mac-docker动态给Container添加端口映射.html">Mac-docker动态给Container添加端口映射</a></li><li class="post-list-item"><a class="post-list-link" href="Vue+SpringBoot项目docker一键部署.html">Vue+SpringBoot项目docker一键部署</a></li><li class="post-list-item"><a class="post-list-link" href="vue-simple-uploader+springboot大文件上传.html">vue-simple-uploader+springboot大文件上传</a></li><li class="post-list-item"><a class="post-list-link" href="IDEA连接Impala.html">IDEA连接Impala</a></li><li class="post-list-item"><a class="post-list-link" href="HBase基本知识.html">HBase基本知识</a></li><li class="post-list-item"><a class="post-list-link" href="IDE破解.html">IDE破解</a></li><li class="post-list-item"><a class="post-list-link" href="salt-api配置.html">salt-api配置</a></li><li class="post-list-item"><a class="post-list-link" href="线上HBase表重建.html">线上HBase表重建</a></li><li class="post-list-item"><a class="post-list-link" href="Alluxio+Presto多版本测试.html">Alluxio+Presto多版本测试</a></li><li class="post-list-item"><a class="post-list-link" href="Linux-top命令详解.html">Linux-top命令详解</a></li><li class="post-list-item"><a class="post-list-link" href="Linux-free命令详解.html">Linux-free命令详解</a></li><li class="post-list-item"><a class="post-list-link" href="hbase的hbck工具.html">hbase的hbck工具</a></li><li class="post-list-item"><a class="post-list-link" href="JanusGraph.html">JanusGraph</a></li><li class="post-list-item"><a class="post-list-link" href="iptables禁用启用端口.html">iptables禁用启用端口</a></li><li class="post-list-item"><a class="post-list-link" href="Phoenix系统表.html">Phoenix系统表</a></li><li class="post-list-item"><a class="post-list-link" href="Phoenix安装问题.html">Phoenix安装问题</a></li><li class="post-list-item"><a class="post-list-link" href="Prometheus安装-HDFS指标收集.html">Prometheus安装+HDFS指标收集</a></li></ul>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/require.js/2.1.6/require.js">
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.9.1/jquery.min.js">
    <script>
        $(".post-list").addClass("toc-article");
        $(".post-list-item a").attr("target","_blank");
        $("#post-nav-button > a:nth-child(2)").click(function() {
            $(".fa-bars, .fa-times").toggle();
            $(".post-list").toggle(300);
            if ($(".toc").length > 0) {
                $("#toc, #tocButton").toggle(200, function() {
                    if ($(".switch-area").is(":visible")) {
                        $("#tocButton").attr("value", valueHide);
                        }
                    })
            }
            else {
            }
        })
    </script>




    <script>
        
    </script>
   <!-- 代码块复制功能-->
   <script src="/js/clipboard-use.js"></script>

</div>
      <footer id="footer">
    <div class="outer">
        <div id="footer-info">
            <div class="footer-left">
                &copy; 2022 月 迹
            </div>
            <div class="footer-right">
                <a href="http://hexo.io/" target="_blank" style="color: #000">Hexo</a>  Theme <a href="https://github.com/luuman/hexo-theme-spfk" target="_blank" style="color: #000">spfk</a> by luuman
            </div>
        </div>
        
            <div class="visit">
                
                    <span id="busuanzi_container_site_pv" style='display:none'>
                        <span id="site-visit" >本站到访数:
                            <span id="busuanzi_value_site_uv"></span>
                        </span>
                    </span>
                
                
                    <span>, </span>
                
                
                    <span id="busuanzi_container_page_pv" style='display:none'>
                        <span id="page-visit">本页阅读量:
                            <span id="busuanzi_value_page_pv"></span>
                        </span>
                    </span>
                
            </div>
        
    </div>
</footer>

    </div>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/require.js/2.1.6/require.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="js/main.js"></script>

    <!--<script>-->
        <!--$(document).ready(function() {-->
            <!--var backgroundnum = 0;-->
            <!--var backgroundimg = "url(/background/bg-" + backgroundnum +".jpg)";-->
            <!--$("#mobile-nav").css({"background-image": backgroundimg,"background-size": "cover","background-position": "center"});-->
            <!--$(".left-col").css({"background-image": backgroundimg,"background-size": "cover","background-position": "center"});-->
        <!--})-->
    <!--</script>-->



<!-- Google Analytics -->
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-168200248-1"></script>
<script>
    window.dataLayer = window.dataLayer || [];
    function gtag(){dataLayer.push(arguments);}
    gtag('js', new Date());

    gtag('config', 'UA-168200248-1');
</script>

<!-- End Google Analytics -->



<script>
	var _hmt = _hmt || [];
	(function() {
		var hm = document.createElement("script");
		hm.src = "https://hm.baidu.com/hm.js?1c0d5d76b7455fc584663ce315f75837";
		var s = document.getElementsByTagName("script")[0];
		s.parentNode.insertBefore(hm, s);
	})();
</script>



<div class="scroll" id="scroll">
    <a href="#"><i class="fa fa-arrow-up"></i></a>
    <a href="#comments"><i class="fa fa-comments-o"></i></a>
    <a href="#footer"><i class="fa fa-arrow-down"></i></a>
</div>
<script>
    $(document).ready(function() {
        if ($("#comments").length < 1) {
            $("#scroll > a:nth-child(2)").hide();
        };
    })
</script>


<script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
    
    <script language="javascript">
    $(function() {
        $("a[title]").each(function() {
            var a = $(this);
            var title = a.attr('title');
            if (title == undefined || title == "") return;
            a.data('title', title).removeAttr('title').hover(

            function() {
                var offset = a.offset();
                $("<div id=\"anchortitlecontainer\"></div>").appendTo($("body")).html(title).css({
                    top: offset.top - a.outerHeight() - 15,
                    left: offset.left + a.outerWidth()/2 + 1
                }).fadeIn(function() {
                    var pop = $(this);
                    setTimeout(function() {
                        pop.remove();
                    }, pop.text().length * 800);
                });
            }, function() {
                $("#anchortitlecontainer").remove();
            });
        });
    });
</script>
    
    
    
        <div class="search-result-list" ></div>
        <div class="search-cover"></div>
    <script type = "text/javascript" >
        $('#search').bind('keypress',function(event){
            if(event.keyCode == "13"){
                search()
            }
        });

    function search() {
        var searchTool = {
            data: [],
            arrList: [],
            oldList: [],
            init() {
                this.fetchJson()
            },
            fetchJson() {
                window.fetch('/content.json?t=' + (+new Date()), {
                    method: 'get',
                }).then((res) => {
                    return res.json()
                }).then((data) => {
                    this.data = data
                    let keywords = $('#search').val()
                    this.searchList(keywords)
                }).catch((err) => {
                    console.log(err)
                });
            },
            // 搜索字符串里面是否存在关键字
            isSreachIndexOF(oldstr, kw) {
                // console.log(oldstr, kw)
                var istrue = false
                // console.log('isSreachIndexOF', oldstr && toString.call(oldstr) === '[object Array]')
                if (oldstr && toString.call(oldstr) === '[object Array]') {
                    for (var i = 0; i < oldstr.length; i++) {
                        oldstr[i].toLowerCase() === kw.toLowerCase() ? istrue = true : null
                    }
                    return istrue
                }
                if (!oldstr || !kw) return false
                istrue = oldstr.toLowerCase().indexOf(kw.toLowerCase()) > -1
                return istrue
            },
            searchList(keywolds) {
                let divList = ''
                if (!keywolds) {
                    if (this.oldList.length) this.clear()
                } else {
                    this.data.forEach((item, index) => {
                        let tagsK = item.tags.map(item => item.name).join('-')
                        let categoriesK = item.categories.map(item => item.name).join('-')
                        if (this.isSreachIndexOF(item.title, keywolds) || this.isSreachIndexOF(tagsK, keywolds) || this.isSreachIndexOF(categoriesK, keywolds) || this.isSreachIndexOF(item.content, keywolds)) {
                            divList += `
                          <li>
                            <a href="/${item.path}" class="search-result-title">${item.title}</a>
                            <div class="main">
                              <div class="left">
                                <span>${item.author ? item.author : 'xyueji'}</span>
                                ${item.tags.map(tags => `<a href="/tags/${tags.slug}" class="search-result-tags">${tags.name}</a>`)}
                              </div>
                              <div class="right">${this.getFriendlyTime(item.date.replace('T', ' '), new Date())}</div>
                            </div>
                          </li>`
                        }
                    })
                    if (this.oldList.length) {
                        this.clear()
                    }
                    if (this.arrList.length) this.add()
                }
                if (!divList) {
                    divList = `<a class="search_close" href="javascript:void(0)" onclick="searchToggle()">×</a> <div style="text-align: center;font-size: 30px; margin-top: 50px;" ">抱歉，未搜索到相关结果</div>`
                } else {
                    divList = `<a class="search_close" href="javascript:void(0)" onclick="searchToggle()">×</a> <ul>` + divList + `</ul>`
                }
                $('.search-result-list').html(divList)
                $('.search-result-list').fadeToggle();
                $('.search-cover').fadeToggle();
            },
            clear() {
                this.oldList.forEach(item => {
                    $(".search-result-list li")[item].style.display = 'none'
                })
                this.oldList = []
            },
            add() {
                this.arrList.forEach(item => {
                    $(".search-result-list li")[item].style.display = 'block'
                })
                this.oldList = this.arrList
            },
            searchGo(keywolds) {
                this.arrList = []
                if (!keywolds) {
                    if (this.oldList.length) this.clear()
                } else {
                    this.data.forEach((item, index) => {
                        if (this.isSreachIndexOF(item.title, keywolds) || this.isSreachIndexOF(item.tags.map(item => item.name).join('-'), keywolds) || this.isSreachIndexOF(item.categories.map(item => item.name).join('-'), keywolds) || this.isSreachIndexOF(item.content, keywolds)) {
                            this.arrList.push(index)
                        }
                    })
                    if (this.oldList.length) this.clear()
                    if (this.arrList.length) this.add()
                }
            },
            /**
             * 获取指定时间的友好时间字符串。
             * @param str 指定的时间字符串，如yyyy-MM-dd HH:mm:ss
             * @param now 当前时间，允许时间戳，GMT时间，如果该参数为undefined，则使用浏览器时间。
             */
            getFriendlyTime(str, now) {
                var currentTime = new Date(now);
                var arr = str.split(/\s+/gi);
                var temp = 0, arr1, arr2, oldTime, delta;
                var getIntValue = function (ss, defaultValue) {
                    try {
                        return parseInt(ss, 10);
                    } catch (e) {
                        return defaultValue;
                    }
                };
                var getWidthString = function (num) {
                    return num < 10 ? ('0' + num) : num;
                };
                if (arr.length >= 2) {
                    arr1 = arr[0].split(/[\/\-]/gi);
                    arr2 = arr[1].split(':');
                    oldTime = new Date();
                    oldTime.setYear(getIntValue(arr1[0], currentTime.getFullYear()));
                    oldTime.setMonth(getIntValue(arr1[1], currentTime.getMonth() + 1) - 1);
                    oldTime.setDate(getIntValue(arr1[2], currentTime.getDate()));

                    oldTime.setHours(getIntValue(arr2[0], currentTime.getHours()));
                    oldTime.setMinutes(getIntValue(arr2[1], currentTime.getMinutes()));
                    oldTime.setSeconds(getIntValue(arr2[2], currentTime.getSeconds()));

                    delta = currentTime.getTime() - oldTime.getTime();

                    if (delta <= 60 * 1000) {
                        return '1分钟内';
                    } else if (delta < 60 * 60 * 1000) {
                        return Math.floor(delta / (60 * 1000)) + '分钟前';
                    } else if (delta < 24 * 60 * 60 * 1000) {
                        return Math.floor(delta / (60 * 60 * 1000)) + '小时前';
                    } else if (delta < 24 * 60 * 60 * 1000 * 30) {
                        return Math.floor(delta / (24 * 60 * 60 * 1000)) + '天前';
                    } else if (delta < 24 * 60 * 60 * 1000 * 30 * 12) {
                        return Math.floor(delta / (24 * 60 * 60 * 1000 * 30)) + '月前';
                    } else if (currentTime.getFullYear() != oldTime.getFullYear()) {
                        return [getWidthString(oldTime.getFullYear()), getWidthString(oldTime.getMonth() + 1), getWidthString(oldTime.getDate())].join('-')
                    } else {
                        return [getWidthString(oldTime.getMonth() + 1), getWidthString(oldTime.getDate())].join('-');
                    }
                }
                return '';
            }
        }
        searchTool.init()
    }
    function searchToggle() {
        $('.search-cover').fadeToggle();
        $('.search-result-list').fadeToggle();
    }
</script>


  </div>
</body>
</html>
